//
//  FilterPriceView.swift
//  koolcar
//
//  Created by xiantian on 2023/11/27.
//

import UIKit

typealias FilterPriceViewBlock = (NSInteger)->Void

class FilterPriceView: UIView {
    var showFlag: Bool?
    var callBack: FilterPriceViewBlock?
    var bkgView: UIButton = {
        let tempView = UIButton()
        tempView.backgroundColor = UIColor.init(hex: 0x000000, alpha: 0.0)
        return tempView
    }()
    var contentView: UIView = {
        let tempView = UIView()
        tempView.backgroundColor = UIColor.white
        return tempView
    }()
    var collectionView: UICollectionView!
    var dataArray: [SellingPriceTypeData] = []
    var selectIndex: NSInteger = 0
    var sellingPriceTypeCode: String = ""
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.clipsToBounds = true
        self.addSubview(bkgView)
        bkgView.addTarget(self, action: #selector(bkgViewAction), for: .touchUpInside)
        self.bkgView.addSubview(contentView)
        
        let layout = UICollectionViewFlowLayout.init()
        layout.scrollDirection = .vertical
        layout.itemSize = CGSize.init(width:(kScreenWidth - 28 - 10)/2, height: 38)
        layout.minimumLineSpacing = 10
        layout.minimumInteritemSpacing = 10
        self.collectionView = UICollectionView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: 200), collectionViewLayout: layout)
        self.collectionView.register(FilterPriceViewItemCell.self, forCellWithReuseIdentifier: "FilterPriceViewItemCell")
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.backgroundColor = UIColor.white
        collectionView.contentInset = UIEdgeInsets.init(top: 10, left: 14, bottom: 10, right: 14)
        self.contentView.addSubview(self.collectionView)
        self.addSnpLayout()
    }
    
    private func addSnpLayout(){
        self.bkgView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        self.contentView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.right.equalTo(0)
        }
        
        collectionView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.top.equalTo(0)
            make.width.equalTo(kScreenWidth)
            make.height.equalTo(200)
            make.bottom.equalTo(contentView.snp.bottom).offset(-10)
        }
    }
    
    @objc func bkgViewAction(){
        self.handleClose()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func startAnimation(){
        //默认选中第
        if self.sellingPriceTypeCode == "" {
            self.selectIndex = 0
        }else{
            let index = self.dataArray.firstIndex(where: {$0.code == Int(self.sellingPriceTypeCode)}) ?? 0
            self.selectIndex = index //+ 1
        }
        self.collectionView.frame.size.height = kScreenHeight
        self.collectionView.reloadData()
        self.collectionView.layoutIfNeeded()
        var tempHeight = self.collectionView.collectionViewLayout.collectionViewContentSize.height
        if tempHeight < 100 {
            tempHeight = 100
        }
        collectionView.snp.updateConstraints { make in
            make.height.equalTo(tempHeight + 20)
        }
        contentView.snp.updateConstraints { make in
            make.top.equalTo(-tempHeight)
        }
        self.layoutIfNeeded()
        UIView.animate(withDuration: 0.4) {
            self.bkgView.backgroundColor = UIColor.init(hex: 0x000000, alpha: 0.6)
            self.contentView.snp.updateConstraints { make in
                make.top.equalTo(0)
            }
            self.layoutIfNeeded()
        } completion: { flag in
            self.showFlag = true
        }
    }
    
    func handleClose(){
        UIView.animate(withDuration: 0.4) {
            self.bkgView.backgroundColor = UIColor.init(hex: 0x000000, alpha: 0)
            self.contentView.snp.updateConstraints { make in
                make.top.equalTo(-self.contentView.frame.size.height)
            }
            self.layoutIfNeeded()
        } completion: { flag in
            if flag {
                self.showFlag = false
                self.removeFromSuperview()
            }
        }
    }
}


extension FilterPriceView: UICollectionViewDataSource, UICollectionViewDelegate {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataArray.count + 1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FilterPriceViewItemCell", for: indexPath) as! FilterPriceViewItemCell
        if self.dataArray.count > indexPath.row  {
            let model = self.dataArray[indexPath.row]
            cell.titleLab.text = model.desc ?? ""
        }else{
            //最后
            cell.titleLab.text = "确定"
        }
        if self.selectIndex == indexPath.row {
            cell.bkgView.layer.borderColor = Theme.mainColor.cgColor
        }else{
            cell.bkgView.layer.borderColor = UIColor.init(hex: 0x939AA8, alpha: 0.12).cgColor
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.row < self.dataArray.count {
            self.selectIndex = indexPath.row
            self.collectionView.reloadData()
        }else{
            //确定
            self.callBack?(self.selectIndex)
            self.handleClose()
        }
    }
}


class FilterPriceViewItemCell: UICollectionViewCell {
    var bkgView: UIView = {
        let tempView = UIView()
        tempView.backgroundColor = Theme.whiteColor
        tempView.layer.cornerRadius = 19
        tempView.layer.masksToBounds = true
        tempView.layer.borderColor = UIColor.init(hex: 0x939AA8, alpha: 0.12).cgColor
        tempView.layer.borderWidth = 1
        return tempView
    }()
    var titleLab: UILabel!
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.contentView.addSubview(bkgView)
        
        titleLab = UILabel()
        titleLab.textColor = Theme.titleLabColor
        titleLab.backgroundColor = UIColor.clear
        titleLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 12)
        titleLab.numberOfLines = 1
        titleLab.lineBreakMode = .byTruncatingTail
        titleLab.textAlignment = .center
        titleLab.adjustsFontSizeToFitWidth = true
        contentView.addSubview(titleLab)
        
        bkgView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        titleLab.snp.makeConstraints { make in
            make.left.equalTo(4)
            make.right.equalTo(-4)
            make.top.equalTo(0)
            make.bottom.equalTo(0)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
